﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using MySql.Data.MySqlClient;

public class DAL_tbl_product_master
{
    public DAL_tbl_product_master()
    {

    }



    public List<tbl_product_master> select(int PRODUCT_ID)
    {
        List<tbl_product_master> list = new List<tbl_product_master>();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "Select PRODUCT_ID,PRODUCT_NAME,IS_ACTIVE,CREATED_BY,CREATED_DATE,UPDATED_BY,UPDATED_DATE from tbl_product_master where PRODUCT_ID=@PRODUCT_ID";
        cmd.Parameters.AddWithValue("@PRODUCT_ID", PRODUCT_ID);
        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        foreach (DataRow dr in dt.Rows)
        {
            list.Add(new tbl_product_master()
            {
                PRODUCT_ID = Convert.ToInt32(dr["PRODUCT_ID"]),
                PRODUCT_NAME = dr["PRODUCT_NAME"].ToString(),
                IS_ACTIVE = Convert.ToBoolean(dr["IS_ACTIVE"]),
                CREATED_BY = dr["CREATED_BY"].ToString(),
                CREATED_DATE = dr["CREATED_DATE"].toDateTimeWithoutError(),
                UPDATED_BY = dr["UPDATED_BY"].ToString(),
                UPDATED_DATE = dr["UPDATED_DATE"].toDateTimeWithoutError()
            });
        }

        return list;
    }



    public List<tbl_product_master> selectAll()
    {
        List<tbl_product_master> list = new List<tbl_product_master>();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "Select PRODUCT_ID,PRODUCT_NAME,IS_ACTIVE,CREATED_BY,CREATED_DATE,UPDATED_BY,UPDATED_DATE from tbl_product_master Order By PRODUCT_ID";

        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        foreach (DataRow dr in dt.Rows)
        {
            list.Add(new tbl_product_master()
            {
                PRODUCT_ID = Convert.ToInt32(dr["PRODUCT_ID"]),
                PRODUCT_NAME = dr["PRODUCT_NAME"].ToString(),
                IS_ACTIVE = Convert.ToBoolean(dr["IS_ACTIVE"]),
                CREATED_BY = dr["CREATED_BY"].ToString(),
                CREATED_DATE = dr["CREATED_DATE"].toDateTimeWithoutError(),
                UPDATED_BY = dr["UPDATED_BY"].ToString(),
                UPDATED_DATE = dr["UPDATED_DATE"].toDateTimeWithoutError()
            });
        }

        return list;
    }



    public int insert(String PRODUCT_NAME, bool IS_ACTIVE, String CREATED_BY, DateTime CREATED_DATE)
    {
        int i = 0;
        string sql = @"Insert into tbl_product_master(PRODUCT_NAME, IS_ACTIVE, CREATED_BY, CREATED_DATE)
                        values(@PRODUCT_NAME, @IS_ACTIVE, @CREATED_BY, @CREATED_DATE)";

        MySqlCommand cmd = new MySqlCommand(sql);

        cmd.Parameters.AddWithValue("@PRODUCT_NAME", PRODUCT_NAME);
        cmd.Parameters.AddWithValue("@IS_ACTIVE", IS_ACTIVE);
        cmd.Parameters.AddWithValue("@CREATED_BY", CREATED_BY);
        cmd.Parameters.AddWithValue("@CREATED_DATE", CREATED_DATE);

        i = DBUtil.ExecuteNonQuery(cmd);
        return i;
    }



    public int update(int PRODUCT_ID, String PRODUCT_NAME, bool IS_ACTIVE, String UPDATED_BY, DateTime UPDATED_DATE)
    {
        int i = 0;
        string sql = @"Update tbl_product_master SET PRODUCT_NAME=@PRODUCT_NAME, IS_ACTIVE=@IS_ACTIVE, UPDATED_BY=@UPDATED_BY, UPDATED_DATE=@UPDATED_DATE WHERE PRODUCT_ID=@PRODUCT_ID";

        MySqlCommand cmd = new MySqlCommand(sql);

        cmd.Parameters.AddWithValue("@PRODUCT_ID", PRODUCT_ID);
        cmd.Parameters.AddWithValue("@PRODUCT_NAME", PRODUCT_NAME);
        cmd.Parameters.AddWithValue("@IS_ACTIVE", IS_ACTIVE);
        cmd.Parameters.AddWithValue("@UPDATED_BY", UPDATED_BY);
        cmd.Parameters.AddWithValue("@UPDATED_DATE", UPDATED_DATE);

        i = DBUtil.ExecuteNonQuery(cmd);
        return i;
    }



    public int delete(int PRODUCT_ID)
    {
        int i = 0;
        string sql = "Delete FROM tbl_product_master WHERE PRODUCT_ID=@PRODUCT_ID";

        MySqlCommand cmd = new MySqlCommand(sql);

        cmd.Parameters.AddWithValue("@PRODUCT_ID", PRODUCT_ID);

        i = DBUtil.ExecuteNonQuery(cmd);
        return i;
    }



}